import { INVALID_VALUE, PRIORITY_DATA } from "../../../Core/CoreDefine";
import { GCtrl } from "../../../Core/GCtrl";
import GParam from "../../../Core/GEvent/GParam";
import { GRunTime } from "../../../Core/Manager/DataPool";
import { CMsg, CURRENCY, ITEM_TYPE, OFFICERITEM } from "../../Common/Define";
import GameMgr from "../../Logic/GameMgr";

const { ccclass } = cc._decorator;
@ccclass
export class RUserData extends GRunTime {
	private _officerLv = INVALID_VALUE;
	public onInit() {
		this.initEvent();
	}

	protected initEvent() {
		GCtrl.ES.on(CMsg.client.bag.onBagItemChange, this, this.onBagItemChange.bind(this), PRIORITY_DATA)
		GCtrl.ES.on(CMsg.client.office.onGetAdReward, this, this.onCMSGGetAdReward.bind(this), PRIORITY_DATA)
		GCtrl.ES.on(CMsg.client.office.onGetReward, this, this.onCMSGGetReward.bind(this), PRIORITY_DATA)
	}


	protected onCMSGGetAdReward(_, param: GParam) {
		let msg = param.get<OfficeGetReward>();
		let status = GameMgr.lUserData.getOfficeStatus(msg.id);
		if (!status) {
			cc.error("onCMSGGetAdReward is error")
		}
		status.adBox = OFFICERITEM.RECEICE;
		GameMgr.lUserData.setOfficeStatus(msg.id, status)
	}

	protected onCMSGGetReward(_, param: GParam) {
		let msg = param.get<OfficeGetReward>();
		let status = GameMgr.lUserData.getOfficeStatus(msg.id);
		if (!status) {
			cc.error("onCMSGGetReward is error")
		}
		status.box = OFFICERITEM.RECEICE;
		GameMgr.lUserData.setOfficeStatus(msg.id, status)

	}

	protected onBagItemChange() {
		this._officerLv = GameMgr.lUserData.officerLv;
		//获取战勋值
		let honor = GameMgr.RBagData.getMegreItems(CURRENCY.FEATS, ITEM_TYPE.CURRENCY);
		//缓存宝箱开启数据
		if (this._officerLv < GameMgr.officerData.data.size - 1) {
			let next = GameMgr.lUserData.getOfficeStatus(this._officerLv + 1);
			let nextRaw = GameMgr.officerData.getRaw<SOfficerDataRaw>(this._officerLv + 1);
			if (nextRaw.exp <= honor.num) {
				next.box = OFFICERITEM.LOCK;
				next.adBox = OFFICERITEM.LOCK;
				this._officerLv += 1;
				GameMgr.lUserData.officerLv = this._officerLv;
				GameMgr.lUserData.setOfficeStatus(this._officerLv, next)
			}
		}
	}
}